package com.boot2.core.utils;

import com.boot2.core.exception.BusinessException;
import lombok.SneakyThrows;
import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;

import java.lang.management.ManagementFactory;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangweilin
 * @ClassName: DateUtil
 * @description: 日志工具
 * @date 2017年11月9日 上午9:43:25
 */
@CommonsLog
public class DateUtil {
    public static final String PATTERN_yyyy_MM_dd = "yyyy-MM-dd";
    public static final String PATTERN_yy_MM_dd = "yy-MM-dd";
    public static final String PATTERN_yyyy_MM = "yyyy-MM";
    public static final String PATTERN_yyyyMMdd = "yyyyMMdd";
    public static final String PATTERN_MMdd = "MMdd";
    public static final String PATTERN_MM_dd = "MM-dd";
    public static final String PATTERN_MM月dd日 = "MM月dd日";
    public static final String PATTERN_yyMMdd = "yy/MM/dd";
    public static final String PATTERN_yyyyMMddHHmmss = "yyyyMMddHHmmss";
    public static final String PATTERN_yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN_MM_dd_HH_mm = "MM-dd HH:mm";
    public static final String PATTERN_yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm";
    public static final String PATTERN_HH_mm_ss = "HH:mm:ss";
    public static final String PATTERN_HHmmss = "HHmmss";
    public static final String PATTERN_yyyyMMdd_HHmmss = "yyyy/MM/dd HH:mm:ss";
    public static final String PATTERN_yyyyMMdd2 = "yyyy/MM/dd";
    public static final String PATTERN_MMdd_HHmm = "MM/dd HH:mm";
    public static final String PATTERTN_HH_mm = "HH:mm";
    public static final String PATTERTN_yyyyMMdd_HH_mm_ss = "yyyyMMdd-HH:mm:ss";
    final static SimpleDateFormat FORMAT = new SimpleDateFormat("yyyyMMdd");
    final static SimpleDateFormat FORMAT2 = new SimpleDateFormat("yyyy/MM/dd");
    public static String[] MONTHS = new String[]{"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"};
    static Log LOG = LogFactory.getLog(DateUtil.class);
    private static int[] DOMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    private static int[] lDOMonth = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    public static final int min = 0;
    public static final int hour = 1;
    public static final int day = 2;
    public static final int week = 3;
    public static final int month = 4;
    public static final int year = 5;


    /**
     * 是否是双休日
     *
     * @param date
     * @return
     */
    public static boolean isWeekend(Date date) {
        int week = getWeekDay(date);
        if (week == 0 || week == 6) {
            return true;
        }
        return false;
    }

    /**
     * 获取星期
     *
     * @param date
     * @return
     */
    public static int getWeekDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int weekDay = cal.get(Calendar.DAY_OF_WEEK);
        if (weekDay > 1) {
            weekDay--;
        } else if (weekDay == 1) {
            weekDay = 7;
        }
        return weekDay;
    }

    /**
     * 添加秒
     *
     * @param date
     * @param sec
     * @return
     */
    public static Date addSec(Date date, int sec) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.SECOND, sec);
        return cal.getTime();
    }

    /**
     * 添加分
     *
     * @param date
     * @param min
     * @return
     */
    public static Date addMin(Date date, int min) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, min);
        return cal.getTime();
    }

    /**
     * 添加时
     *
     * @param date
     * @param hour
     * @return
     */
    public static Date addHour(Date date, int hour) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR_OF_DAY, hour);
        return cal.getTime();
    }

    /**
     * 添加天
     *
     * @param date
     * @param day
     * @return
     */
    public static Date addDay(Date date, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, day);
        return cal.getTime();
    }

    /**
     * 添加星期
     *
     * @param date
     * @param day
     * @return
     */
    public static Date addWeek(int week) {
        return addWeek(new Date(), week);
    }

    /**
     * 添加星期
     *
     * @param date
     * @param day
     * @return
     */
    public static Date addWeek(Date date, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.WEEK_OF_YEAR, day);
        return cal.getTime();
    }

    /**
     * 添加月
     *
     * @param date
     * @param month
     * @return
     */
    public static Date addMonth(Date date, int month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, month);
        return cal.getTime();
    }

    /**
     * 添加年
     *
     * @param date
     * @param year
     * @return
     */
    public static Date addYear(Date date, int year) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.YEAR, year);
        return cal.getTime();
    }

    /**
     * 添加秒
     *
     * @param sec
     * @return
     */
    public static Date addSec(int sec) {
        return addSec(new Date(), sec);
    }

    /**
     * 添加分
     *
     * @param min
     * @return
     */
    public static Date addMin(int min) {
        return addMin(new Date(), min);
    }

    /**
     * 添加时
     *
     * @param hour
     * @return
     */
    public static Date addHour(int hour) {
        return addHour(new Date(), hour);
    }

    /**
     * 添加天
     *
     * @param day
     * @return
     */
    public static Date addDay(int day) {
        return addDay(new Date(), day);
    }

    /**
     * 添加月
     *
     * @param month
     * @return
     */
    public static Date addMonth(int month) {
        return addMonth(new Date(), month);
    }

    /**
     * 添加年
     *
     * @param year
     * @return
     */
    public static Date addYear(int year) {
        return addYear(new Date(), year);
    }

    /**
     * 添加秒
     *
     * @param date
     * @param sec
     * @return
     */
    public static Date setSec(Date date, int sec) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.SECOND, sec);
        return cal.getTime();
    }

    /**
     * 添加分
     *
     * @param date
     * @param min
     * @return
     */
    public static Date setMin(Date date, int min) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.MINUTE, min);
        return cal.getTime();
    }

    /**
     * 添加时
     *
     * @param date
     * @param hour
     * @return
     */
    public static Date setHour(Date date, int hour) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        return cal.getTime();
    }

    /**
     * 添加天
     *
     * @param date
     * @param day
     * @return
     */
    public static Date setDay(Date date, int week) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, week);
        return cal.getTime();
    }

    /**
     * 添加星期
     *
     * @param date
     * @param day
     * @return
     */
    public static Date setWeek(int week) {
        return setWeek(new Date(), week);
    }

    /**
     * 添加星期
     *
     * @param date
     * @param day
     * @return
     */
    public static Date setWeek(Date date, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.WEEK_OF_YEAR, day);
        return cal.getTime();
    }

    /**
     * 添加月
     *
     * @param date
     * @param month
     * @return
     */
    public static Date setMonth(Date date, int month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.MONTH, month);
        return cal.getTime();
    }

    /**
     * 添加年
     *
     * @param date
     * @param year
     * @return
     */
    public static Date setYear(Date date, int year) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.YEAR, year);
        return cal.getTime();
    }

    /**
     * 添加秒
     *
     * @param sec
     * @return
     */
    public static Date setSec(int sec) {
        return setSec(new Date(), sec);
    }

    /**
     * 添加分
     *
     * @param min
     * @return
     */
    public static Date setMin(int min) {
        return setMin(new Date(), min);
    }

    /**
     * 添加时
     *
     * @param hour
     * @return
     */
    public static Date setHour(int hour) {
        return setHour(new Date(), hour);
    }

    /**
     * 添加天
     *
     * @param day
     * @return
     */
    public static Date setDay(int day) {
        return setDay(new Date(), day);
    }

    /**
     * 添加月
     *
     * @param month
     * @return
     */
    public static Date setMonth(int month) {
        return setMonth(new Date(), month);
    }

    /**
     * 添加年
     *
     * @param year
     * @return
     */
    public static Date setYear(int year) {
        return setYear(new Date(), year);
    }

    /**
     * 取秒
     *
     * @param date
     * @return
     */
    public static int getSec(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        Object min = cal.get(Calendar.SECOND);
        if (min != null) {
            return Integer.valueOf(min.toString());
        } else {
            return 0;
        }
    }

    /**
     * 取分
     *
     * @param date
     * @return
     */
    public static int getMin(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        Object min = cal.get(Calendar.MINUTE);
        if (min != null) {
            return Integer.valueOf(min.toString());
        } else {
            return 0;
        }
    }


    /**
     * 取小时
     *
     * @param date
     * @return
     */
    public static int getHour(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        Object hour = cal.get(Calendar.HOUR_OF_DAY);
        if (hour != null) {
            return Integer.valueOf(hour.toString());
        } else {
            return 0;
        }
    }

    /**
     * 取日期是一个月中的几号.
     *
     * @param date
     * @return
     */
    public static int getDay(String date) {
        return getDay(getFormatDate(date, PATTERN_yyyy_MM_dd));
    }

    /**
     * 取日期是一个月中的几号.
     *
     * @param date
     * @return
     */
    public static int getDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        Object day = cal.get(Calendar.DATE);
        if (day != null) {
            return Integer.valueOf(day.toString());
        } else {
            return 0;
        }
    }

    /**
     * 获取年
     *
     * @param date
     * @return
     */
    public static int getYear(String date) {
        return getYear(getFormatDate(date, PATTERN_yyyy_MM_dd));
    }


    /**
     * 取日期是哪一年
     *
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        Object year = cal.get(Calendar.YEAR);
        if (year != null) {
            return Integer.valueOf(year.toString());
        } else {
            return 0;
        }
    }

    public static String getDateStr(int year, int month, int day, String patten) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, month - 1, day);
        SimpleDateFormat dateformat = new SimpleDateFormat(patten);
        return dateformat.format(cal.getTime());
    }

    public static String getDateStr(long year, long month) {
        String monthStr = month < 10 ? "0" + month : "" + month;
        return year + "-" + monthStr;
    }

    public static Date getDate(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, month - 1, day);
        return cal.getTime();
    }

    /**
     * 通过时分秒构建时间
     *
     * @param hour
     * @param min
     * @param sec
     * @return
     */
    public static Date getTime(int hour, int min, int sec) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, min);
        cal.set(Calendar.SECOND, sec);
        return cal.getTime();
    }

    /**
     * 判断两个时间的大小.
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean isCompareTime(Date startTime, Date endTime) {
        if (endTime.getTime() > startTime.getTime()) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 判断两个时间的大小 1大于 -1小于 0等于
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static int compareTime(Date startTime, Date endTime) {
        if (endTime.getTime() > startTime.getTime()) {
            return -1;
        } else if (endTime.getTime() < startTime.getTime()) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 判断两个时间的大小 1大于 -1小于 0等于
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static int compareTime(String startTime, String endTime) {
        Date time1 = getFormatDate(startTime, PATTERN_yyyy_MM_dd_HH_mm_ss);
        Date time2 = getFormatDate(endTime, PATTERN_yyyy_MM_dd_HH_mm_ss);
        return compareTime(time1, time2);
    }

    /**
     * 取日期是月份.
     *
     * @param date
     * @return
     */
    public static int getMonth(String date) {
        return getMonth(getFormatDate(date, PATTERN_yyyy_MM_dd));
    }

    /**
     * 取日期是月份.
     *
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // cal.add(Calendar.MONTH, 1);
        Object day = cal.get(Calendar.MONTH);
        if (day != null) {
            return Integer.valueOf(day.toString()) + 1;
        } else {
            return 0;
        }
    }

    public static int getDaysOfmonth(int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, month);
        if ((cal.get(Calendar.YEAR) % 4) == 0) {
            if ((cal.get(Calendar.YEAR) % 100) == 0 && (cal.get(Calendar.YEAR) % 400) != 0) {
                return DOMonth[cal.get(Calendar.MONTH)];
            }
            return lDOMonth[cal.get(Calendar.MONTH)];
        } else {
            return DOMonth[cal.get(Calendar.MONTH)];
        }
    }

    public static Calendar getClearCalendar() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal;
    }

    public static Date getDateAfterDays(Date date, int duration) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, duration);
        return cal.getTime();
    }

    public static Date getDateBeforeHours(Date date, int duration) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR_OF_DAY, -duration);
        return cal.getTime();
    }

    public static Date getDateAfterMinutes(long duration) {
        long curr = System.currentTimeMillis();
        curr = curr + duration * 60 * 1000;
        return new Date(curr);
    }

    public static String formatDate(Date date, String format) {
        return getFormatDate(date, format);
    }

    public static String formatDate(Date date) {
        return getFormatDate(date, PATTERN_yyyy_MM_dd_HH_mm_ss);
    }

    public static String formatNow() {
        return formatDate(now());
    }

    /**
     * 格式时间
     *
     * @param date
     * @param format
     * @return
     */
    public static String getFormatDate(Date date, String format) {
        if (date != null) {
            SimpleDateFormat f = new SimpleDateFormat(format);
            return f.format(date);
        } else {
            return null;
        }
    }

    /**
     * 格式时间
     *
     * @param date
     * @param format
     * @return
     */
    @SneakyThrows
    public static Date warpDate(Date date, String format) {
        if (date != null) {
            SimpleDateFormat f = new SimpleDateFormat(format);
            String dateStr = f.format(date);
            return f.parse(dateStr);
        } else {
            return null;
        }
    }

    /**
     * 将String型格式化,比如想要将2011-11-11格式化成2011年11月11日,就StringPattern("2011-11-11",
     * "yyyy-MM-dd","yyyy年MM月dd日").
     *
     * @param date       String 想要格式化的日期
     * @param oldPattern String 想要格式化的日期的现有格式
     * @param newPattern String 想要格式化成什么格式
     * @return String
     */
    public static String StringPattern(String date, String oldPattern, String newPattern) throws Exception {
        if (date == null || oldPattern == null || newPattern == null) {
            return "";
        }
        SimpleDateFormat sdf1 = new SimpleDateFormat(oldPattern); // 实例化模板对象
        SimpleDateFormat sdf2 = new SimpleDateFormat(newPattern); // 实例化模板对象
        Date d = null;
        d = sdf1.parse(date); // 将给定的字符串中的日期提取出来
        return sdf2.format(d);
    }

    /**
     * 格式时间
     *
     * @param date
     * @param format
     * @return
     * @throws Exception
     */
    public static Date getFormatDate(String date, String format) {
        if (date != null) {
            SimpleDateFormat f = new SimpleDateFormat(format);
            try {
                return f.parse(date);
            } catch (ParseException e) {
                log.error("日期转换异常:", e);
                return null;
            }
        } else {
            return null;
        }
    }

    /**
     * 格式时间
     *
     * @param date
     * @param format
     * @return
     * @throws Exception
     */
    public static Date getFormatDate(String date) {
        if (date != null) {
            SimpleDateFormat f = new SimpleDateFormat(PATTERN_yyyy_MM_dd_HH_mm_ss);
            try {
                return f.parse(date);
            } catch (ParseException e) {
                log.error("日期转换异常:", e);
                return null;
            }
        } else {
            return null;
        }
    }

    public static String getZHDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        switch (calendar.get(Calendar.DAY_OF_WEEK)) {
            case Calendar.MONDAY:
                return "星期一";
            case Calendar.TUESDAY:
                return "星期二";
            case Calendar.WEDNESDAY:
                return "星期三";
            case Calendar.THURSDAY:
                return "星期四";
            case Calendar.FRIDAY:
                return "星期五";
            case Calendar.SATURDAY:
                return "星期六";
            case Calendar.SUNDAY:
                return "星期日";
            default:
                return "";
        }
    }

    /**
     * 当前日期小时相加或相减所得日期（+,-）操作,输入一个日期得到天数加减后的日期。
     *
     * @param cal
     * @return
     */
    public static Date DsDay_Hour(Date date, Integer hours) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hours);

        return cal.getTime();
    }

    /**
     * 把参数时间精确到天
     *
     * @param cal
     * @return
     */

    public static Date accurateToDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return clearDateAfterDay(cal);
    }

    /**
     * 得到每月1号是星期几
     *
     * @param cal
     * @return
     */
    public static int getDayOfWeekOf1(Calendar cal) {
        cal.set(Calendar.DATE, 1);
        return cal.get(Calendar.DAY_OF_WEEK);
    }

    public static Date getTheMiddle(Date date, int plus) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, 1);
        cal.add(Calendar.MONTH, plus);
        return cal.getTime();
    }

    public static Map<String, Object> getBeginAndEndDateByDate(Date date) {

        Calendar calClearDate = Calendar.getInstance();
        calClearDate.setTime(date);
        calClearDate.set(Calendar.DATE, 1);
        date = calClearDate.getTime();
        Map<String, Object> map = new HashMap<String, Object>();
        // SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        int dayOfWeek = getDayOfWeekOf1(cal);
        cal.set(Calendar.DATE, -(dayOfWeek - 2));
        map.put("beginDate", cal.getTime());
        cal.add(Calendar.DATE, 21);
        map.put("currPageDate", cal.getTime());
        cal.add(Calendar.DATE, 20);
        map.put("endDate", cal.getTime());
        return map;
    }

    /**
     * 根据格式获取日期字符串.
     *
     * @param format
     * @param aDate
     * @return
     */
    public static String getDateTime(String format, Date aDate) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        String returnValue = df.format(aDate);
        return returnValue;
    }

    /**
     * 当前日期分钟相加或相减所得日期（+,-）操作,输入一个日期得到分钟加减后的日期。
     *
     * @param months
     * @return
     */
    public static Date DsDay_Minute(Date date, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int minutes = calendar.get(Calendar.MINUTE);
        calendar.set(Calendar.MINUTE, minutes + minute);
        return calendar.getTime();
    }

    /**
     * 清理所有天后面的日期时间
     *
     * @param c
     * @return
     */
    public static Date clearDateAfterDay(Calendar c) {
        c.set(Calendar.MILLISECOND, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.HOUR_OF_DAY, 0);
        return c.getTime();
    }

    /**
     * String转Date
     *
     * @param sdate    日期字符串
     * @param fmString 指定日期格式
     * @return
     */
    public static Date toDate(String sdate, String fmString) {
        DateFormat df = new SimpleDateFormat(fmString);
        try {
            return df.parse(sdate);
        } catch (ParseException e) {
            throw new RuntimeException("日期格式不正确 ");
        }
    }

    /**
     * 根据出生日期得到年龄
     */
    public static int getAge(Date birthDay) {
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthDay)) {
            return 0;
        }

        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;// 注意此处，如果不加1的话计算结果是错误的
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;
        if (monthNow < monthBirth || (monthNow == monthBirth && dayOfMonthNow < dayOfMonthBirth)) {
            age--;
        }
        return age;
    }

    // 计算两个日期之间有多少天
    public static int getDaysBetween(Date startDate, Date endDate) {
        Calendar d1 = Calendar.getInstance();
        d1.setTime(startDate);
        Calendar d2 = Calendar.getInstance();
        d2.setTime(endDate);
        if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
            Calendar swap = d1;
            d1 = d2;
            d2 = swap;
        }
        int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
        int y2 = d2.get(Calendar.YEAR);
        if (d1.get(Calendar.YEAR) != y2) {
            d1 = (Calendar) d1.clone();
            do {
                days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);// 得到当年的实际天数
                d1.add(Calendar.YEAR, 1);
            } while (d1.get(Calendar.YEAR) != y2);
        }
        return days;
    }

    /**
     * 当前日期相加或相减所得日期（+,-）操作
     *
     * @param months
     * @return Date
     */
    public static Date dsDayDate(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int days = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE, days + day);
        Date cc = calendar.getTime();
        return cc;
    }

    /**
     * 当前日期相加或相减所得日期（+,-）操作 小时
     *
     * @param months
     * @return Date
     */
    public static Date dsHourDate(Date date, int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int hours = calendar.get(Calendar.HOUR);
        calendar.set(Calendar.HOUR, hours + hour);
        Date cc = calendar.getTime();
        return cc;
    }

    /**
     * 取年.
     *
     * @param date
     * @return
     */
    public static String getFormatYear(Date date) {
        String str = null;
        if (date != null) {
            str = DateFormatUtils.format(date, "yyyy");
        }
        return str;
    }

    /**
     * 得到前一天晚上6点钟.
     *
     * @return
     * @throws ParseException
     */
    public static Date getYestoday18Hour() throws ParseException {
        Date d = new Date();
        SimpleDateFormat simpleOldDate = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar ca = Calendar.getInstance();
        ca.setTime(simpleOldDate.parse(simpleOldDate.format(d)));
        ca.add(Calendar.DATE, -1);
        ca.add(Calendar.HOUR, 18);
        return ca.getTime();
    }

    public static Date getAfterDay(Date date) {
        SimpleDateFormat simpleOldDate = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar ca = Calendar.getInstance();
        try {
            ca.setTime(simpleOldDate.parse(simpleOldDate.format(date)));
        } catch (ParseException e) {
            ca.setTime(getDayStart(new Date()));
        }
        ca.add(Calendar.DATE, 1);
        return ca.getTime();

    }

    public static Date getBeforeDay(Date date) {
        SimpleDateFormat simpleOldDate = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar ca = Calendar.getInstance();
        try {
            ca.setTime(simpleOldDate.parse(simpleOldDate.format(date)));
        } catch (ParseException e) {
            ca.setTime(getDayStart(new Date()));
        }
        ca.add(Calendar.DATE, -1);
        return ca.getTime();

    }

    /**
     * 返回 该日期的开始处
     *
     * @param date
     * @return
     */
    public static Date getDayStart(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    public static Date getDayEndTime(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTime();
    }

    /**
     * 得到当前时间的23点.
     *
     * @param date
     * @return
     */
    public static Date getDayEndBeforeOneH(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 00);
        c.set(Calendar.SECOND, 00);
        c.set(Calendar.MILLISECOND, 000);
        return c.getTime();
    }

    /**
     * date1是否早于date2
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean inAdvance(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return false;
        }
        return date1.getTime() < date2.getTime();
    }

    /**
     * 将日期和时分的两个时间合并到一起
     *
     * @param date
     * @param time
     * @return
     */
    public static Date mergeDateTime(Date date, Date time) {
        Calendar dateCalendar = Calendar.getInstance();
        dateCalendar.setTime(date);
        Calendar timeCalendar = Calendar.getInstance();
        timeCalendar.setTime(time);
        dateCalendar.set(Calendar.HOUR, timeCalendar.get(Calendar.HOUR));
        dateCalendar.set(Calendar.MINUTE, timeCalendar.get(Calendar.MINUTE));
        dateCalendar.set(Calendar.SECOND, timeCalendar.get(Calendar.SECOND));
        return dateCalendar.getTime();
    }

    public static Date getDateByStr(String dateStr, String formate) {
        SimpleDateFormat sdf = new SimpleDateFormat(formate);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error(e);
        }
        return date;
    }

    /**
     * 字符串转换到时间格式
     *
     * @param dateStr   需要转换的字符串
     * @param formatStr 需要格式的目标字符串 举例 yyyy-MM-dd
     * @return Date 返回转换后的时间
     * @throws ParseException 转换异常
     */
    public static Date stringToDate(String dateStr, String formatStr) {
        DateFormat sdf = new SimpleDateFormat(formatStr);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            throw new BusinessException(e);
//            LOG.error(e);
        }
        return date;
    }

    /**
     * 比较2个日期 前一个日期至少比后一个日期大一天以上
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean compareDateLessOneDayMore(Date date1, Date date2) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date1);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(date2);
        if (c1.get(Calendar.YEAR) > c2.get(Calendar.YEAR)) {
            return true;
        } else if (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) && c1.get(Calendar.MONTH) > c2.get(Calendar.MONTH)) {
            return true;
        } else if (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) && c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH)
                && c1.get(Calendar.DATE) > c2.get(Calendar.DATE)) {
            return true;
        }
        return false;
    }

    /**
     * 将日期加年.
     *
     * @param date
     * @param time
     * @return
     */
    public static Date mergeDateTimeAddYear(Date date, Integer years) {
        Calendar dateCalendar = Calendar.getInstance();
        dateCalendar.setTime(date);
        dateCalendar.set(Calendar.YEAR, dateCalendar.get(Calendar.YEAR) + years);
        return dateCalendar.getTime();
    }

    /**
     * 将日期加月.
     *
     * @param date
     * @param time
     * @return
     */
    public static Date mergeDateTimeAddMonth(Date date, Integer Months) {
        Calendar dateCalendar = Calendar.getInstance();
        dateCalendar.setTime(date);
        dateCalendar.set(Calendar.MONTH, dateCalendar.get(Calendar.MONTH) + Months);
        return dateCalendar.getTime();
    }

    /**
     * 将日期加天.
     *
     * @param date
     * @param time
     * @return
     */
    public static Date mergeDateTimeAddDay(Date date, Integer days) {
        Calendar dateCalendar = Calendar.getInstance();
        dateCalendar.setTime(date);
        dateCalendar.set(Calendar.DATE, dateCalendar.get(Calendar.DATE) + days);
        return dateCalendar.getTime();
    }

    public static Long getMinBetween(Date startDate, Date endDate) {
        Calendar d1 = Calendar.getInstance();
        d1.setTime(startDate);
        Calendar d2 = Calendar.getInstance();
        d2.setTime(endDate);
        Long min = 0L;
        if (d1.getTimeInMillis() > d2.getTimeInMillis()) {
            min = (d1.getTimeInMillis() - d2.getTimeInMillis()) / (1000 * 60);
        } else {
            min = (d2.getTimeInMillis() - d1.getTimeInMillis()) / (1000 * 60);
        }
        return min;
    }

    /**
     * 相差秒数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static Long getMillisBetween(Date startDate, Date endDate) {
        Calendar d1 = Calendar.getInstance();
        d1.setTime(startDate);
        Calendar d2 = Calendar.getInstance();
        d2.setTime(endDate);
        Long min = 0L;
        if (d1.getTimeInMillis() > d2.getTimeInMillis()) {
            min = (d1.getTimeInMillis() - d2.getTimeInMillis()) / 1000;
        } else {
            min = (d2.getTimeInMillis() - d1.getTimeInMillis()) / 1000;
        }
        return min;
    }

    /**
     * 获取今天的日期，去掉时、分、秒
     */
    public static Date getTodayYMDDate() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);// 24小时制
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 获取今天的日期，去掉时、分、秒
     */
    public static Date getTodayYMDDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);// 24小时制
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * @param
     * @return Date
     * @description 获取开始时间
     */
    public static Date getBeginTime() {
        return getTodayYMDDate();
    }

    /**
     * @param
     * @return Date
     * @description 获取开始时间
     */
    public static Date getBeginTime(Date date) {
        return getTodayYMDDate(date);
    }

    /**
     * @param
     * @return Date
     * @description 获取结束时间
     */
    public static Date getEndTime() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 23);// 24小时制
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTime();
    }

    /**
     * 得到今天几点
     *
     * @return
     */
    public static int getTodayHourDate() {
        Calendar cal = Calendar.getInstance();
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        return hour;
    }

    /**
     * 获取今天的日期yyyy-MM-dd格式
     *
     * @return
     */
    public static Date getDate() {
        return stringToDate(formatDate(new Date(), "yyyy-MM-dd"), "yyyy-MM-dd");
    }

    /**
     * 获取今天的日期yyyy-MM-dd格式
     *
     * @return
     */
    public static Date getDate(Date date) {
        return stringToDate(formatDate(date, "yyyy-MM-dd"), "yyyy-MM-dd");
    }

    /**
     * 获取今天的日期yyyy-MM-dd格式
     *
     * @return
     */
    public static String getDateStr(Date date) {
        return formatDate(date, "yyyy-MM-dd");
    }

    /**
     * 将分钟转成小时数,有小误差
     */
    public static float convertToHours(Long minutes) {
        if (minutes == null) {
            return 0f;
        }
        String fStr = (new Float(minutes + "") / 60) + "";
        String res = fStr.substring(0, fStr.lastIndexOf(".") + 2);
        return Float.parseFloat(res);
    }

    /**
     * 将小时数转为分钟,有小误差
     */
    public static long convertToMinutes(Float hours) {
        if (hours == null) {
            return 0L;
        }
        long h = hours.intValue() * 60;
        Float f = (hours - new Float(hours.intValue())) * 60;
        String fStr = f.toString();
        return h + Long.parseLong(fStr.substring(0, fStr.indexOf(".")));
    }

    /**
     * 年月日时分秒的日期转只有年月日的日期
     */
    public static Date toYMDDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);// 24小时制
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 本月第一天
     */
    public static Date getFirstdayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }

    /**
     * 本月最后一天
     */
    public static Date getLastdayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, value);
        date = cal.getTime();
        return date;
    }

    /**
     * date 减去当前日期 . 剩余0天0时0分
     *
     * @return str
     */
    public static String getRemainTimeByCurrentDate(Date date) {
        String str = "剩余0天0时0分";
        if (null != date) {
            Date d = new Date();
            long seconds = (date.getTime() - d.getTime()) / 1000;
            if (seconds > 0) { // 秒
                long day = seconds / (3600 * 24); // 天数
                long house = (seconds % (3600 * 24)) / 3600; // 小时
                long min = (seconds % (3600)) / 60;// 分
                return "剩余" + day + "天" + house + "时" + min + "分";
            }

        }
        return str;
    }

    /**
     * 得到一个时间的具体日期 例如：两个月后的前一个星期 month = 3 ; day = -7
     *
     * @param DATE
     * @return DATE
     * @author zhongshuangxi
     */
    public static Date getTimesByTimes(int Month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MONTH, Month);
        cal.add(Calendar.DATE, day);
        return cal.getTime();
    }

    /**
     * 返回当前时间 毫秒
     *
     * @param date
     * @return
     */
    public static long getDateTime(String date) {
        if (StringUtils.isEmpty(date)) {
            return 0L;
        }
        Date d = toDate(date, "yyyy-MM-dd");
        return d.getTime();
    }

    /**
     * 比较2个日期 前一个日期至少比后一个日期大一天以上
     *
     * @param date1
     * @param date2
     * @return
     */
    public static String compareDate(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return null;
        }
        if (date1.getTime() - date2.getTime() < 0) {
            return null;
        }
        long seconds = (date1.getTime() - date2.getTime()) / 1000;
        Long day = seconds / (3600 * 24); // 天数
        Long hour = (seconds % (3600 * 24)) / 3600; // 小时
        Long min = (seconds % (3600)) / 60;// 分
        String str = "";
        if (day > 0) {
            str = str + day + "天";
        }
        if (hour > 0) {
            str = str + hour + "小时";
        }
        if (min > 0) {
            str = str + min + "分";
        }
        return str;
    }

    /**
     * 在特定的日期上，加入特定的时间点，返回新的日期
     *
     * @param date 特定日期
     * @param time 给入的时间点 time 的格式举例：09:00
     * @return
     */
    public static Date defineDate(Date date, String time) throws Exception {
        try {
            String[] strs = time.split(":");
            int hour = Integer.parseInt(strs[0]);
            int minute = Integer.parseInt(strs[1]);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.set(Calendar.HOUR_OF_DAY, hour);
            c.set(Calendar.MINUTE, minute);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            return c.getTime();
        } catch (Exception e) {
            throw new Exception("给入的时间点参数格式错误！");
        }
    }

    public static List<Date> converFromString(String addition) {
        String[] ds = addition.split(",");
        List<Date> dateList = new ArrayList<Date>();
        for (String d : ds) {
            d = StringUtils.trimToNull(d);
            if (d == null) {
                continue;
            }
            try {
                dateList.add(FORMAT.parse(d));
            } catch (ParseException e) {
                LOG.error(e);
            }
        }
        Collections.sort(dateList);
        return dateList;
    }

    public static String convertDate(Date specDate) {
        return FORMAT.format(specDate);
    }

    public static Date converDateFromStr(String str) throws ParseException {
        return FORMAT.parse(str);
    }

    public static Date converDate4ymd(String str) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN_yyyy_MM_dd);
        return dateFormat.parse(str);
    }

    public static Date converDate(String str, String format) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.parse(str);
    }

    public static Date converDateFromStr2(String str) throws ParseException {
        return FORMAT2.parse(str);
    }

    public static Date getBeforeDayBySecond(int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) - second);
        return calendar.getTime();
    }

    public static List<String> getLimitDateByWeek(String beginDate, String endDate, String weeks) {
        List<String> dates = new ArrayList<String>();
        Date begin = DateUtil.getDateByStr(beginDate, "yyyy-MM-dd");
        Date end = DateUtil.getDateByStr(endDate, "yyyy-MM-dd");
        while (!begin.after(end)) {
            Calendar c = Calendar.getInstance();
            c.setTime(begin);
            int day = c.get(Calendar.DAY_OF_WEEK) - 1;
            if (weeks.contains("" + day)) {
                dates.add(DateUtil.formatDate(begin, "yyyy-MM-dd"));
            }
            begin = DateUtil.dsDayDate(begin, 1);
        }
        return dates;
    }

    /**
     * 计算两个日期的天数
     *
     * @param smdate
     * @param bdate
     * @return
     * @throws ParseException
     */
    public static int daysBetween(String smdate, String bdate) {
        long between_days = 0;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            cal.setTime(sdf.parse(smdate));
            long time1 = cal.getTimeInMillis();
            cal.setTime(sdf.parse(bdate));
            long time2 = cal.getTimeInMillis();
            between_days = (time2 - time1) / (1000 * 3600 * 24);
        } catch (Exception ex) {
            LOG.error(ex);
        }
        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 当前日期按月份相加之后所得日期
     *
     * @param date
     * @param month
     * @return
     */
    public static Date getDateMonth(Date date, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int monthTest = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, monthTest + month);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date cc = calendar.getTime();
        return cc;
    }

    public static Date changeDateMinute(Date date, int iMinute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, iMinute);
        return calendar.getTime();
    }

    /**
     * @param
     * @return Date
     * @description 返回计算后的时间
     */
    public static Date getCalculateDate(Date date, int calendarEnum, int value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendarEnum, value);
        return calendar.getTime();
    }

    /**
     * 转换微信DateTime时间
     *
     * @param duration
     * @return
     */
    public static String formatDate(long createTime) {
        long msgCreateTime = createTime * 1000L;
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date(msgCreateTime));
    }

    /**
     * 获取今天还剩下多少秒
     *
     * @return
     */
    public static int getLastSecondsOfToday() {
        Calendar curDate = Calendar.getInstance();
        Calendar tommorowDate = new GregorianCalendar(curDate.get(Calendar.YEAR), curDate.get(Calendar.MONTH), curDate.get(Calendar.DATE) + 1, 0, 0, 0);
        return (int) (tommorowDate.getTimeInMillis() - curDate.getTimeInMillis()) / 1000;
    }

    /**
     * 判断日期是否在某段日期之间
     *
     * @param BeginDate
     * @param endDate
     * @param date
     * @return
     */
    public static boolean betweenAnd(Date BeginTime, Date endTime, Date date) {
        if (DateUtil.isCompareTime(BeginTime, date) && DateUtil.isCompareTime(date, endTime)) {
            return true;
        }
        return false;
    }

    /**
     * 按分钟向小的数值粘附时间,如以5分钟为例,得到
     * 8:03:35 -->8:00:00
     * 8:21:35 -->8:20:00
     * 8:28:32 -->8:25:00
     * 8:58:32 -->8:55:00
     *
     * @param date
     * @param mins 粘附粒度
     * @return
     */
    public static Date adhesionMin(Date date, int mins) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int minute = calendar.get(Calendar.MINUTE);
        int floorMinute = (minute / mins) * mins;
        calendar.set(Calendar.MINUTE, floorMinute);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 拿到的数据日期和当前时间比较
     *
     * @param dateStr
     * @return
     */
    public static boolean compareDateStr(String dateStr) {
        Timestamp today = getTodayTimeStamp();
//        logger.info("今天的时间：{}",today);
        Timestamp dateTimeStamp = today;
        if (dateStr.length() == 10) {
            dateTimeStamp = Timestamp.valueOf(dateStr + " 00:00:00");
        } else if (dateStr.length() == 7) {
            dateTimeStamp = Timestamp.valueOf(dateStr + "-01 00:00:00");
        } else if (dateStr.length() == 4) {
            dateTimeStamp = Timestamp.valueOf(dateStr + "-01-01 00:00:00");
        } else if (dateStr.length() > 10) {
            //部分时间不正常，不进行下一步的保存，例如：CRA4745E4F,start_date:2099-0-1-0-1,end_date:2018-02-07
            return true;
        }
        if (dateTimeStamp.after(today)) {
//            log.info("拿到的数据日期大于今天日期，不进行下一步数据的保存！");
            return true;
        } else if (dateTimeStamp.equals(today)) {
//            logger.info("拿到的数据日期等于今天日期，下一步进行数据的获取保存到数据库");
            return false;
        } else {
//            logger.info("拿到的数据日期小于今天日期，可以进行获取数据");
            return false;
        }
    }

    /**
     * 获取当天0点时间戳
     *
     * @return
     */
    public static Timestamp getTodayTimeStamp() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * Description 获取时分字符串
     */
    public static String getHMOfDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        String hourStr = hour < 10 ? "0" + hour : "" + hour;
        int minute = calendar.get(Calendar.MINUTE);
        String minuteStr = minute < 10 ? "0" + minute : "" + minute;
        return hourStr + ":" + minuteStr;
    }


    /**
     * 获取今天
     *
     * @return String
     */
    public static Date getToday() {
        return new Date();
    }

    /**
     * 获取昨天
     *
     * @return String
     */
    public static Date getYesterday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, -1);
        Date time = cal.getTime();
        return time;
    }

    /**
     * 获取本月开始日期
     *
     * @return String
     **/
    public static Date getMonthStartDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date time = cal.getTime();
        return time;
    }

    /**
     * 获取本月最后一天
     *
     * @return String
     **/
    public static Date getMonthEndDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date time = cal.getTime();
        return time;
    }

    /**
     * 获取本周的第一天
     *
     * @return String
     **/
    public static Date getWeekStartDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.WEEK_OF_MONTH, 0);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        Date time = cal.getTime();
        return time;
    }

    /**
     * 获取本周的最后一天
     *
     * @return String
     **/
    public static Date getWeekEndDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
        cal.add(Calendar.DAY_OF_WEEK, 1);
        Date time = cal.getTime();
        return time;
    }

    //=====以下2019年3月3日集成

    /**
     * 获取当天的开始时间
     *
     * @param date
     * @return
     */
    public static Date getDayBeginTime(Date date) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }


    /**
     * 获取昨天的开始时间
     *
     * @param date
     * @return
     */
    public static Date getBeginDayOfYesterday(Date date) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBeginTime(date));
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }

    /**
     * 获取昨天的结束时间
     *
     * @param date
     * @return
     */
    public static Date getEndDayOfYesterDay(Date date) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEndTime(date));
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }

    /**
     * 获取明天的开始时间
     *
     * @param date
     * @return
     */
    public static Date getBeginDayOfTomorrow(Date date) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBeginTime(date));
        cal.add(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }

    /**
     * 获取明天的结束时间
     *
     * @param date
     * @return
     */
    public static Date getEndDayOfTomorrow(Date date) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEndTime(date));
        cal.add(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }

    /**
     * 获取本周的开始时间
     *
     * @param date
     * @return
     */
    @SuppressWarnings("unused")
    public static Date getBeginDayOfWeek(Date date) {
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return getDayStartTime(cal.getTime());
    }

    /**
     * 获取本周的结束时间
     *
     * @param date
     * @return
     */
    public static Date getEndDayOfWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfWeek(date));
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }

    /**
     * 获取上周的开始时间
     *
     * @param date
     * @return
     */
    @SuppressWarnings("unused")
    public static Date getBeginDayOfLastWeek(Date date) {
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek - 7);
        return getDayStartTime(cal.getTime());
    }

    /**
     * 获取上周的结束时间
     *
     * @param date
     * @return
     */
    public static Date getEndDayOfLastWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfLastWeek(date));
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }

    /**
     * 获取本月的开始时间
     *
     * @return
     */
    public static Date getBeginDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(getNowYear(date), getNowMonth(date) - 1, 1);
        return getDayStartTime(calendar.getTime());
    }

    /**
     * 获取本月的结束时间
     *
     * @param date
     * @return
     */
    public static Date getEndDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(getNowYear(date), getNowMonth(date) - 1, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(date), getNowMonth(date) - 1, day);
        return getDayEndTime(calendar.getTime());
    }

    /**
     * 获取上月的开始时间
     *
     * @param date
     * @return
     */
    public static Date getBeginDayOfLastMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(getNowYear(date), getNowMonth(date) - 2, 1);
        return getDayStartTime(calendar.getTime());
    }

    /**
     * 获取上月的结束时间
     *
     * @param date
     * @return
     */
    public static Date getEndDayOfLastMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(getNowYear(date), getNowMonth(date) - 2, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(date), getNowMonth(date) - 2, day);
        return getDayEndTime(calendar.getTime());
    }

    /**
     * 获取本年的开始时间
     *
     * @param date
     * @return
     */
    public static Date getBeginDayOfYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.YEAR, getNowYear(date));
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        cal.set(Calendar.DATE, 1);
        return getDayStartTime(cal.getTime());
    }

    /**
     * 获取本年的结束时间
     *
     * @param date
     * @return
     */
    public static Date getEndDayOfYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.YEAR, getNowYear(date));
        cal.set(Calendar.MONTH, Calendar.DECEMBER);
        cal.set(Calendar.DATE, 31);
        return getDayEndTime(cal.getTime());
    }

    /**
     * 获取某个日期的开始时间
     *
     * @param date
     * @return
     */
    public static Timestamp getDayStartTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        if (null != date) {
            calendar.setTime(date);
        }
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 获取今年是哪一年
     *
     * @param date
     * @return
     */
    public static Integer getNowYear(Date date) {
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return Integer.valueOf(gc.get(1));
    }

    /**
     * 获取本月是哪一月
     *
     * @param date
     * @return
     */
    public static int getNowMonth(Date date) {
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return gc.get(2) + 1;
    }

    /**
     * 两个日期相减得到的天数
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static int getDiffDays(Date beginDate, Date endDate) {
        if (beginDate == null || endDate == null) {
            throw new IllegalArgumentException("getDiffDays param is null!");
        }
        long diff = (endDate.getTime() - beginDate.getTime())
                / (1000 * 60 * 60 * 24);
        int days = new Long(diff).intValue();
        return days;
    }

    /**
     * 两个日期相减得到的毫秒数
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static long dateDiff(Date beginDate, Date endDate) {
        long date1ms = beginDate.getTime();
        long date2ms = endDate.getTime();
        return date2ms - date1ms;
    }

    /**
     * 计算两个时间相差的格式化的值
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static String dateDiffStr(Date beginDate, Date endDate) {
        long dateDiff = dateDiff(beginDate, endDate);

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;

        // 计算差多少天
        long day = dateDiff / nd;
        // 计算差多少小时
        long hour = dateDiff % nd / nh;
        // 计算差多少分钟
        long min = dateDiff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * 获取两个日期中的最大日期
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static Date max(Date beginDate, Date endDate) {
        if (beginDate == null) {
            return endDate;
        }
        if (endDate == null) {
            return beginDate;
        }
        if (beginDate.after(endDate)) {
            return beginDate;
        }
        return endDate;
    }

    /**
     * 获取两个日期中的最小日期
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static Date min(Date beginDate, Date endDate) {
        if (beginDate == null) {
            return endDate;
        }
        if (endDate == null) {
            return beginDate;
        }
        if (beginDate.after(endDate)) {
            return endDate;
        }
        return beginDate;
    }

    /**
     * 返回某月该季度的第一个月
     *
     * @param date
     * @return
     */
    public static Date getFirstSeasonDate(Date date) {
        final int[] SEASON = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int sean = SEASON[cal.get(Calendar.MONTH)];
        cal.set(Calendar.MONTH, sean * 3 - 3);
        return cal.getTime();
    }

    /**
     * 返回某个日期下几天的日期
     *
     * @param date
     * @param i
     * @return
     */
    public static Date getNextDay(Date date, int i) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);
        return cal.getTime();
    }

    /**
     * 返回某个日期前几天的日期
     *
     * @param date
     * @param i
     * @return
     */
    public static Date getFrontDay(Date date, int i) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
        return cal.getTime();
    }

    /**
     * 获取某年某月到某年某月按天的切片日期集合(间隔天数的集合)
     *
     * @param beginYear
     * @param beginMonth
     * @param endYear
     * @param endMonth
     * @param k
     * @return
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static List getTimeList(int beginYear, int beginMonth, int endYear,
                                   int endMonth, int k) {
        List list = new ArrayList();
        if (beginYear == endYear) {
            for (int j = beginMonth; j <= endMonth; j++) {
                list.add(getTimeList(beginYear, j, k));
            }
        } else {
            {
                for (int j = beginMonth; j < 12; j++) {
                    list.add(getTimeList(beginYear, j, k));
                }
                for (int i = beginYear + 1; i < endYear; i++) {
                    for (int j = 0; j < 12; j++) {
                        list.add(getTimeList(i, j, k));
                    }
                }
                for (int j = 0; j <= endMonth; j++) {
                    list.add(getTimeList(endYear, j, k));
                }
            }
        }
        return list;
    }

    /**
     * 获取某年某月按天切片日期集合(某个月间隔多少天的日期集合)
     *
     * @param beginYear
     * @param beginMonth
     * @param k
     * @return
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static List getTimeList(int beginYear, int beginMonth, int k) {
        List list = new ArrayList();
        Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);
        int max = begincal.getActualMaximum(Calendar.DATE);
        for (int i = 1; i < max; i = i + k) {
            list.add(begincal.getTime());
            begincal.add(Calendar.DATE, k);
        }
        begincal = new GregorianCalendar(beginYear, beginMonth, max);
        list.add(begincal.getTime());
        return list;
    }

    /**
     * 是否只有日期
     *
     * @param date
     * @return
     */
    public static boolean isOnlyHasDate(Date date) {
        int hour = getHour(date);
        int min = getMin(date);
        int sec = getSec(date);
        if (hour == 0 && min == 0 && sec == 0) {
            return true;
        }
        return false;
    }

    /**
     * 是否只有时间
     *
     * @param date
     * @return
     */
    public static boolean isOnlyHasTime(Date date) {
        int year = getYear(date);
        int month = getMonth(date);
        int day = getDay(date);
        if (year == 1970 && month == 1 && day == 1) {
            return true;
        }
        return false;
    }


    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    public static Date now() {
        return new Date();
    }

    //==========
    public static Map<String, Object> currentDay() {
        Map<String, Object> time = new HashMap<String, Object>();
        DateTime dateTime = new DateTime();
        String startTime = dateTime.toString("yyyy-MM-dd 00:00:00");
        String endTime = dateTime.toString("yyyy-MM-dd HH:00:00");
        time.put("startTime", startTime);
        time.put("endTime", endTime);
        return time;
    }

    public static Map<String, Object> currentMonth() {
        Map<String, Object> time = new HashMap<String, Object>();
        DateTime dateTime = new DateTime();
        String startTime = dateTime.toString("yyyy-MM-01 00:00:00");
        dateTime = dateTime.plusMonths(1);
        String endTime = dateTime.toString("yyyy-MM-01 00:00:00");
        time.put("startTime", startTime);
        time.put("endTime", endTime);
        return time;
    }

    public static Map<String, Object> currentYear() {
        Map<String, Object> time = new HashMap<String, Object>();
        DateTime dateTime = new DateTime();
        String startTime = dateTime.toString("yyyy-01-01 00:00:00");
        dateTime = dateTime.plusYears(1);
        String endTime = dateTime.toString("yyyy-01-01 00:00:00");
        time.put("startTime", startTime);
        time.put("endTime", endTime);
        return time;
    }

    public static Map<String, Object> lastYear() {
        Map<String, Object> time = new HashMap<String, Object>();
        DateTime dateTime = new DateTime();
        String endTime = dateTime.toString("yyyy-MM-dd HH:ss:00");
        dateTime = dateTime.plusMonths(-12);
        String startTime = dateTime.toString("yyyy-MM-dd HH:ss:00");
        time.put("startTime", startTime);
        time.put("endTime", endTime);
        return time;
    }


}
